package lab2weightgui;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSpinner;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 * Title: WeightGUI class for CSIS 235 Lab 2
 * Description: CS 235 Lab #2
 *    Basic GUI class to keep track of the weight of something important.
 * 
 * @author Anthony Varghese
 * @email anthony.varghese@my.uwrf.edu
 * @date September 3rd 2014
 * @version 1.0
 */
public class WeightGUI extends JFrame implements ChangeListener{

	/**
	 * main - this is where the program starts
	 * @param args
	 */
	public static void main(String[] args) {
		new WeightGUI( "Weights" );
	}

	/**
	 * Class data members:
	 * Default serial version id - for serialization
	 * Constants for positioning the window and placing GUI objects in window
	 */
	private static final long serialVersionUID = 1L;
	private static final int WINDOW_X_POSITION = 200;
	private static final int WINDOW_Y_POSITION = 200;
	private static final int WINDOW_X_SIZE     = 400;
	private static final int WINDOW_Y_SIZE     = 300;
	private static final int POUNDS_SPINNER_X_POSITION = 100;
	private static final int POUNDS_SPINNER_Y_POSITION = 100;
	private static final int POUNDS_SPINNER_X_SIZE     = 60;
	private static final int POUNDS_SPINNER_Y_SIZE     = 30;
	private static final int OUNCES_SPINNER_X_POSITION = POUNDS_SPINNER_X_POSITION + POUNDS_SPINNER_X_SIZE + 10;
	private static final int OUNCES_SPINNER_Y_POSITION = POUNDS_SPINNER_Y_POSITION;
	private static final int OUNCES_SPINNER_X_SIZE     = POUNDS_SPINNER_X_SIZE;
	private static final int OUNCES_SPINNER_Y_SIZE     = POUNDS_SPINNER_Y_SIZE;
	private static final int LABEL_X_POSITION = POUNDS_SPINNER_X_POSITION;
	private static final int LABEL_Y_POSITION = POUNDS_SPINNER_Y_POSITION + POUNDS_SPINNER_Y_SIZE + 30;
	private static final int LABEL_X_SIZE     = 200;
	private static final int LABEL_Y_SIZE     = POUNDS_SPINNER_Y_SIZE;
	
	
	/**
	 * Instance data members
	 */
	private JSpinner spinPounds, spinOunces;
	private JLabel   weightLabel;

	/**
	 * Constructor
	 * @param title
	 */
	public WeightGUI(String title) {
		super(title);
		
		/*
		 * Set the size of the window
		 */
		setBounds( WINDOW_X_POSITION, WINDOW_Y_POSITION, WINDOW_X_SIZE, WINDOW_Y_SIZE );

		/*
		 * Do the layout ourselves
		 */
		setLayout( null );
		setDefaultCloseOperation( EXIT_ON_CLOSE );

		/*
		 * Set up two spinners:
		 */
		spinPounds = new JSpinner( );
		spinPounds.setBounds( POUNDS_SPINNER_X_POSITION, POUNDS_SPINNER_Y_POSITION, POUNDS_SPINNER_X_SIZE, POUNDS_SPINNER_Y_SIZE );
		add( spinPounds );
		spinPounds.addChangeListener( this );

		spinOunces = new JSpinner( );
		spinOunces.setBounds( OUNCES_SPINNER_X_POSITION, OUNCES_SPINNER_Y_POSITION, OUNCES_SPINNER_X_SIZE, OUNCES_SPINNER_Y_SIZE );
		add( spinOunces );
		spinOunces.addChangeListener( this );
		
		/*
		 * Set up the result label
		 */
		weightLabel = new JLabel("Weight is 0");
		weightLabel.setBounds( LABEL_X_POSITION, LABEL_Y_POSITION, LABEL_X_SIZE, LABEL_Y_SIZE );
		add( weightLabel );
		
		setVisible( true );
	}

	@Override
	public void stateChanged(ChangeEvent e) {
		weightLabel.setText("Weight is now " + spinPounds.getValue() + " lbs. " + spinOunces.getValue() + " oz." );
		
	}
}
